import os
import json

class ContainerProperties:
    """获取容器信息"""

    container_id = None
    image_id = None
    status = None
    container_name = None
    env = None
    cmd = None
    ip_address = None
    gateway = None
    mac = None

    def __init__(self, container_name=str):
        self.get_properties(container_name)

    def get_properties(self, container_name=str):
        """获取容器属性"""
        result = os.popen("docker inspect {} 2> /dev/null".format(container_name))
        container_info_list = result.read().strip()
        if container_info_list == '[]':
            return None
        container_info = json.loads(container_info_list)[0]
        
        self.container_id = container_info['Id']
        self.container_name = container_name
        self.image_id = str(container_info['Image'][7:])
        self.status = container_info['State']['Status']
        self.env = container_info['Config']['Env']
        self.cmd = container_info['Config']['Cmd']
        self.ip_address = container_info['NetworkSettings']['IPAddress']
        self.gateway = container_info['NetworkSettings']['Gateway']
        self.mac = container_info['NetworkSettings']['MacAddress']